<!DOCTYPE html>
<html lang="zh_CN" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="utf-8">
    <title>模型管理 List</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, module-scalable=0">
    <link rel="stylesheet" th:href="@{/statics/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/statics/style/admin.css}" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-form layui-card">
                <div class="layui-card-header">模型列表</div>
                <div class="layui-card-body">
                    <table class="layui-hide" id="module-list-table" lay-filter="module-list-filter"></table>

                    <script type="text/html" id="module-list-table-operate-bar">
                        <a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="view">查看</a>
                        {{#  if(d.isSystem != '1'){ }}
                        <a shiro:hasPermission="dev:module:edit" class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                        <a shiro:hasPermission="dev:module:remove" class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
                        {{#  } }}
                    </script>
                    <script type="text/html" id="module-list-table-switch-status-tpl">
                        <input type="checkbox" name="status" lay-skin="switch" lay-filter="module-list-table-status"
                               value="{{ d.id }}" data-json="{{ encodeURIComponent(JSON.stringify(d)) }}" {{ d.status == 1 ? 'checked' : '' }}>
                    </script>

                    <script type="text/html" id="module-list-table-toolbar">
                        <div class="layui-btn-container">
                            <button shiro:hasPermission="dev:module:add" class="layui-btn layui-btn-sm" lay-event="add">
                                添加模型
                            </button>
                        </div>
                    </script>

                </div>
            </div>
        </div>
    </div>
</div>


<script th:src="@{/statics/layui/layui.js}"></script>
<script th:inline="none">
    layui.config({
        base: '/statics/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table'], function () {
        var $ = layui.$
            , admin = layui.admin
            , form = layui.form
            , table = layui.table;

        var tableIns = table.render({
            elem: '#module-list-table'
            , url: '/api/module/list'
            , id: 'id'
            , toolbar: '#module-list-table-toolbar'
            , cols: [[
                {field: 'id', width: 60, title: '序号'}
                , {field: 'name', width: 150, title: '名称', sort: true}
                , {field: 'code', width: 100, title: '代码'}
                , {
                    field: 'type', width: 80, title: '类型', templet: function (d) {
                        if(d.type == 1){
                            return '单页';
                        }else{
                            return '列表';
                        }
                    }
                }
                , {field: 'listView', title: '列表视图', templet: function (d) {
                        if(d.listView == 'default'){
                            return '默认列表';
                        }else if(d.listView == 'thumb'){
                            return '默认视图';
                        }else {
                            return '自定义视图(' + d.listView +')';
                        }
                    }
                }
                , {field: 'tabs', title: '字段分组'}
                , {field: 'status', width: 80, title: '状态', fixed: 'right', templet: '#module-list-table-switch-status-tpl'}
                , {width: 170, align: 'left', fixed: 'right', toolbar: '#module-list-table-operate-bar'}
            ]]
        });

        //头工具栏事件
        table.on('toolbar(module-list-filter)', function (obj) {
            var checkStatus = table.checkStatus('id');
            switch (obj.event) {
                case 'add':
                    layer.open({
                        type: 2
                        , title: '添加模型'
                        , content: '/cms/dev/module/add'
                        , maxmin: true
                        , area: ['600px', '450px']
                        , btn: ['确定', '取消']
                        , yes: function (index, layero) {
                            var iframeWindow = window['layui-layer-iframe' + index]
                                , submitID = 'LAY-module-add-submit'
                                , submit = layero.find('iframe').contents().find('#' + submitID);

                            //监听提交
                            iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                                var field = data.field; //获取提交的字段
                                if(field.status == 'on'){
                                    field.status = 1;
                                }else {
                                    field.status = 0;
                                }
                                //提交 Ajax 成功后，静态更新表格中的数据
                                $.ajax({
                                    url: '/api/module/add',
                                    method: 'post',
                                    data: field,
                                    dataType: 'JSON',
                                    success: function (res) {
                                        if (res.code == '0') {
                                            tableIns.reload(); //数据刷新
                                            layer.close(index); //关闭弹层
                                        } else {
                                            alert(res.msg);
                                        }
                                    },
                                    error: function (err) {
                                    }
                                });

                            });

                            submit.trigger('click');
                        }
                    });
                    break;
            }
            ;
        });

        //监听行工具事件
        table.on('tool(module-list-filter)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'del':
                    $.ajax({
                        url: '/api/module/records?id=' + data.id,
                        method: 'get',
                        success: function (res) {
                            if(res.code == 0){
                                deleteConfirm(data.id , res.data);
                            }
                        }
                    });

                    break;
                case 'edit':
                    layer.open({
                        type: 2
                        , title: '编辑模型'
                        , content: '/cms/dev/module/edit?id=' + data.id
                        , maxmin: true
                        , area: ['600px', '450px']
                        , btn: ['确定', '取消']
                        , yes: function (index, layero) {
                            var iframeWindow = window['layui-layer-iframe' + index]
                                , submitID = 'LAY-module-add-submit'
                                , submit = layero.find('iframe').contents().find('#' + submitID);

                            //监听提交
                            iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                                var field = data.field; //获取提交的字段
                                if(field.status == 'on'){
                                    field.status = 1;
                                }else {
                                    field.status = 0;
                                }
                                //提交 Ajax 成功后，静态更新表格中的数据
                                $.ajax({
                                    url: '/api/module/edit',
                                    method: 'post',
                                    data: field,
                                    dataType: 'JSON',
                                    success: function (res) {
                                        if (res.code == '0') {
                                            tableIns.reload(); //数据刷新
                                            layer.close(index); //关闭弹层
                                        } else {
                                            alert(res.msg);
                                        }
                                    },
                                    error: function (err) {
                                    }
                                });

                            });

                            submit.trigger('click');
                        }
                    });
                    break;
                case 'view':
                    layer.open({
                        type: 2
                        , title: '模型信息'
                        , content: '/cms/dev/module/get?id=' + data.id
                        , maxmin: true
                        , area: ['600px', '450px']
                        , btn: ['关闭']
                    });
                    break;
            }
        });

        form.on("switch(module-list-table-status)",function(obj){
            layer.load();
            var statusInt = obj.elem.checked ? 1 : 0;
            $.ajax({
                url: '/api/module/status',
                method: 'post',
                data: {id : this.value , status : statusInt},
                dataType: 'JSON',
                success: function (res) {
                    layer.closeAll();
                    if (res.code == '0') {
                        layer.msg('数据修改完成!', {icon: 1,time: 2000});
                    } else {
                        layer.msg('ERROR:数据修改错误!', {icon: 2,time: 2000});
                    }
                },
                error: function (err) {
                    layer.closeAll();
                    layer.msg('数据修改错误', {icon: 2,time: 2000});
                }
            });
        });

        function deleteConfirm(id,count){
            layer.confirm('本模型有' + count　+'条内容记录，确定删除此模型吗？', function (index) {
                $.ajax({
                    url: '/api/module/removeFlag?id=' + id,
                    method: 'put',
                    contentType: "application/json;charset=UTF-8",
                    dataType: 'JSON',
                    success: function (res) {
                        if (res.code == '0') {
                            layer.msg('相关记录已删除!', {icon: 1, time: 1000}, function () {
                                location.reload();
                                layer.close(index); //关闭弹层
                            });
                        } else {
                            layer.msg(res.msg);
                        }
                    },
                    error: function (err) {
                    }
                });
            });
        }

    });
</script>
</body>
</html>